/*************************************************************
 * Title: WordFilterChainHelper.java
 * Description: 
 * Author: Huang Shaobin
 * Email: shaobin.software@gmail.com
 * CreateTime: Oct 15, 2013 2:45:01 PM
 * Description:
 ************************************************************/
package snow.core.toolkit.filter;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;

/**
 *
 */
public class WordFilterChainHelper {

	private static final WordFilterChain rootChain = new WordFilterChain();

	public static void addKeyword(List<String> keywords) {
		if (CollectionUtils.isNotEmpty(keywords)) {
			for (String keyword : keywords) {
				keyword = StringUtils.trim(keyword);
				int length = StringUtils.length(keyword);
				WordFilterChain nextChain = rootChain;
				for (int index = 0; index < length; index++) {
					Character word = Character.valueOf(keyword.charAt(index));
					WordFilterChain chain = nextChain.getChainFrom(word);
					if (chain == null) {
						chain = new WordFilterChain();
						nextChain.putChainFrom(word, chain);
						nextChain = chain;
					} else {
						nextChain = chain;
					}
					if (index == length - 1) {
						chain.setEnding(true);
					}
				}
			}
		}
	}

	private static int checkKeyWord(String inputText, int fromIndex, int flag) {
		int maxMatch = -1;
		inputText = StringUtils.trim(inputText);
		int length = StringUtils.length(inputText);
		int offset = 0;
		WordFilterChain thisChain = rootChain;
		for (int index = fromIndex > 0 ? fromIndex : 0; index < length; index++) {
			Character word = Character.valueOf(inputText.charAt(index));
			WordFilterChain nextChain = thisChain.getChainFrom(word);
			if (nextChain == null) {
				break;
			}
			offset++;
			thisChain = nextChain;
			if (nextChain.isEnding()) {
				maxMatch = offset;
				if (flag == 1) {
					break;
				}
			}
		}
		return maxMatch;
	}

	public static void clear() {
		rootChain.clear();
	}

	public static boolean containsKeyword(String inputText) {
		inputText = StringUtils.trim(inputText);
		int length = StringUtils.length(inputText);
		for (int index = 0; index < length; index++) {
			int len = checkKeyWord(inputText, index, 1);
			if (len > 0) {
				return true;
			}
		}
		return false;
	}

	public static List<String> getKeywordsFrom(String inputText) {
		List<String> keywordList = new ArrayList<String>();
		Set<String> keywordSet = new HashSet<String>();
		inputText = StringUtils.trim(inputText);
		int length = StringUtils.length(inputText);
		for (int index = 0; index < length; index++) {
			int len = checkKeyWord(inputText, index, 1);
			if (len > 0) {
				String keyword = inputText.substring(index, index + len);
				if (keywordSet.add(keyword)) {
					keywordList.add(keyword);
				}
			}
		}
		return keywordList;
	}
}
